package com.aijk.web.admin.controller.ehm;


import com.aijk.commons.utils.PasswordHelper;
import com.aijk.ehm.sys.api.SysAdminApi;
import com.aijk.ehm.sys.model.SysAdmin;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.utils.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/ehm/update")
public class PasController extends BaseController {
    @Autowired
    private SysAdminApi sysAdminApi;

    @RequestMapping(value = "/pas", method = RequestMethod.GET)
    public String list_view(Model model) {
        return "ehm/pas_edit";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(String passWord) {
        RetMsg retMsg = new RetMsg();
        try{
            SysAdmin sysAdmin = sysAdminApi.get(getCurrentUser().getAdminId());
            sysAdmin.setSalt(RandomString.generateRandomString(16));
            sysAdmin.setPassword(PasswordHelper.encryptPassword(passWord, sysAdmin.getSalt()));
            sysAdminApi.update(sysAdmin);
            retMsg.put("code" , 0);
        }catch (Exception e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return retMsg;
    }


}
